﻿using BDQN.Dtos;
using BDQN.IBLL;
using Microsoft.AspNetCore.Mvc;
using X.PagedList;
using X.PagedList.Extensions;
using BDQN.Commons;
using System.Security.Cryptography.X509Certificates;
using BDQN.WebApp.Areas.manage.Models.Roles;
using System.Text;
namespace BDQN.WebApp.Areas.manage.Controllers
{
    [Area("manage")]
    public class RolesManagerController : Controller
    {
        // 注入BLL层的对象
        private IRolesBll _rolesBll;
        public RolesManagerController(IRolesBll rolesBll)
        {
            _rolesBll = rolesBll;
        }

        [HttpGet]
        public async Task<IActionResult> List(string? search, int? pageIndex)
        {
            search = search ?? string.Empty;
            int currentIndex = pageIndex ?? 1;
            var data = await _rolesBll.GetRolesAsync(search);
            IPagedList<RolesDto> pages = data.ToPagedList(currentIndex, PageConfig.PageSize);
            ViewBag.Search = search;
            ViewBag.PageIndex = currentIndex;
            return View(pages);



        }
        [HttpGet]
        public IActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Add(RolesAddViewModel roles)
        {
            if (ModelState.IsValid)
            {
                var res = await _rolesBll.AddRolesAsync(roles.Title);
                if (res > 0)
                    return Content("<script>alert('新增成功');location.href='/manage/RolesManager/List'</script>", "text/HTML", Encoding.UTF8);
            }
            return View(roles);
        }

        [HttpGet]
        public async Task<IActionResult> Edit(int id)
        {
            var model = await _rolesBll.GetRolesAsync(id);
            if (model == null || string.IsNullOrEmpty(model.Title))
            {
                return Content("<script>alert('该条数据不存在');location.href='/manage/RolesManager/List'</script>", "text/HTML", Encoding.UTF8);
            }
            return View(new RolesEditViewModel { Id = model.Id, Title = model.Title });
        }

        [HttpPost]
        public async Task<IActionResult> Edit(RolesEditViewModel roles)
        {
            if (ModelState.IsValid)
            {
                var res = await _rolesBll.EditRolesAsync(roles.Id, roles.Title);
                if (res > 0)
                    return Content("<script>alert('修改成功');location.href='/manage/RolesManager/List'</script>", "text/HTML", Encoding.UTF8);
            }
            return View(roles);
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            var res = await _rolesBll.DeleteRolesAsync(id);
            string msg = res > 0 ? "success" : "failed";
            return Json(msg);
        }
        [HttpPost]
        public async Task<IActionResult> RemoveList(int[] idList)
        {
            var res = await _rolesBll.DeleteRolesListAsync(idList);
            string msg = res >= idList.Length ? "success" : "failed";
            return Json(msg);
        }
    }
}
